package com.kucai.yifaqapp.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;

/**
 * Created by admin on 2018/12/13.
 */

public class KeyBoardWeight extends RelativeLayout implements View.OnClickListener {
    private RelativeLayout goback;
    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;
    private TextView text5;
    private TextView text6;
    private TextView text7;
    private TextView text8;
    private TextView text9;
    private TextView text10;
    private TextView text11;
    private ImageView text12;
    private String content;
    private Goback mgoback;
    private EditText medittext;
    StringBuffer buffer = new StringBuffer();

    public KeyBoardWeight(Context context) {
        this(context, null);
    }

    public KeyBoardWeight(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.layout_virtual_keyboard, null);
        initView(view);
        initData();
        initEvnet();
        addView(view);      //必须要，不然不显示控件
    }

    public void init(EditText text, Goback mback) {
//        if (android.os.Build.VERSION.SDK_INT <= 10) {
//            medittext.setInputType(InputType.TYPE_NULL);
//        } else {
//            context.getWindow().setSoftInputMode(
//                    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//            try {
//                Class<EditText> cls = EditText.class;
//                Method setShowSoftInputOnFocus;
//                setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",
//                        boolean.class);
//                setShowSoftInputOnFocus.setAccessible(true);
//                setShowSoftInputOnFocus.invoke(medittext, false);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        this.medittext = text;
        this.mgoback = mback;
    }

    private void initEvnet() {
    }

    private void initData() {
    }

    private void initView(View view) {
        goback = view.findViewById(R.id.goback);
        text1 = view.findViewById(R.id.text1);
        text2 = view.findViewById(R.id.text2);
        text3 = view.findViewById(R.id.text3);
        text4 = view.findViewById(R.id.text4);
        text5 = view.findViewById(R.id.text5);
        text6 = view.findViewById(R.id.text6);
        text7 = view.findViewById(R.id.text7);
        text8 = view.findViewById(R.id.text8);
        text9 = view.findViewById(R.id.text9);
        text10 = view.findViewById(R.id.text10);
        text11 = view.findViewById(R.id.text11);
        text12 = view.findViewById(R.id.text12);
        goback.setOnClickListener(this);
        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);
        text4.setOnClickListener(this);
        text5.setOnClickListener(this);
        text6.setOnClickListener(this);
        text7.setOnClickListener(this);
        text8.setOnClickListener(this);
        text9.setOnClickListener(this);
        text10.setOnClickListener(this);
        text11.setOnClickListener(this);
        text12.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text1:
                if(buffer.length()<15){
                    buffer.append("1");
                    medittext.setText(buffer.toString());
                }

                break;
            case R.id.text2:
                if(buffer.length()<15){
                    buffer.append("2");
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.text3:
                if(buffer.length()<15){
                    buffer.append("3");
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.text4:
                if(buffer.length()<15){
                    buffer.append("4");
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.text5:
                if(buffer.length()<15){
                    buffer.append("5");
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.text6:
                if(buffer.length()<15){
                    buffer.append("6");
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.text7:
                if(buffer.length()<15){
                    buffer.append("7");
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.text8:
                if(buffer.length()<15){
                    buffer.append("8");
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.text9:
                if(buffer.length()<15){
                    buffer.append("9");
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.text10:
//                buffer.append(".");
//                medittext.setText(buffer.toString());
                Toast.makeText(getContext(), "目前只支持整数下注", Toast.LENGTH_SHORT).show();
                break;
            case R.id.text11:

                if(buffer.length()==0){

                }else {
                    if(buffer.length()<15){
                        buffer.append("0");
                        medittext.setText(buffer.toString());
                    }
                }
                break;
            case R.id.text12:
                if (buffer.length() > 0) {
                    buffer.deleteCharAt(buffer.length() - 1);
                    medittext.setText(buffer.toString());
                }
                break;
            case R.id.goback:
                mgoback.goback();
                break;
        }
    }

    public interface Goback {
        void goback();
    }

    public interface Delete {
        void delete();
    }
}
